//
//  SpecialPromoteViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/12.
//  Copyright © 2017年 Imon. All rights reserved.
//
//
import UIKit

class SpecialPromoteViewController: UIViewController {

    @IBOutlet weak var moreButton: UIButton!
    
    var scrollView: UIScrollView?
    
    let promotions: [RestaurantPromotion] = {
        var promotions: [RestaurantPromotion] = []
        for i in 1...9 {
            let p = RestaurantPromotion()
            promotions.append(p)
        }
        return promotions
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        scrollView = UIScrollView()
        scrollView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        scrollView?.showsVerticalScrollIndicator = false
        scrollView?.showsHorizontalScrollIndicator = false
        scrollView?.isScrollEnabled = true
        scrollView?.autoresizingMask = UIViewAutoresizing.flexibleHeight
        scrollView?.backgroundColor = lightSaperatorColor
        
        let topInset: CGFloat = 15
        let leading: CGFloat = 10
        let gap: CGFloat = 8
        let width: CGFloat = (self.view.bounds.width - leading * 2 - gap * 2) / 3
        let height: CGFloat = width + 35
        
        for i in 0...promotions.count - 1 {
            let h = i % 3
            let v = i / 3
            promotions[i].frame = CGRect(x: leading + CGFloat(h) * (width + gap), y: topInset + CGFloat(v) * (height + gap), width: width, height: height)
            promotions[i].profilePic.image = UIImage(named: "r\(h + 1)")
            promotions[i].name.text = "酒樓特價大優惠\(i + 1)"
            let tapGesture = MyTapGestureRecognizer()
            tapGesture.id = i
            tapGesture.addTarget(self, action: #selector(tapPromotion(_:)))
            promotions[i].addGestureRecognizer(tapGesture)
            scrollView?.addSubview(promotions[i])
        }
        
        let maxSize = CGSize(width: self.view.bounds.width , height: 75)
        let buttonSize = TextSize.textSize(text: (moreButton.titleLabel?.text)!, font: (moreButton.titleLabel?.font)!, maxSize: maxSize)
        moreButton.frame = CGRect(x: self.view.bounds.width - 15 - buttonSize.width, y: promotions[7].frame.origin.y + promotions[7].frame.height + 20, width: buttonSize.width, height: buttonSize.height)
        moreButton.removeFromSuperview()
        scrollView?.addSubview(moreButton)
        scrollView?.contentSize = CGSize(width: self.view.bounds.width, height: (self.scrollView?.frame.height)! + 0.5)
        self.view.addSubview(scrollView!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tapPromotion(_ sender: MyTapGestureRecognizer) {
//        let detailPage = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "RestaurantDetailViewController") as! RestaurantDetailViewController
//        detailPage.restaurant = Restaurant(profilePic: UIImage(named: "r1"), district: "", distance: "", name: "優惠酒樓\(sender.id! + 1)", highPrice: 6767, lowPrice: 6766)
//        detailPage.hidesBottomBarWhenPushed = true
//        self.navigationController?.pushViewController(detailPage, animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

class MyTapGestureRecognizer: UITapGestureRecognizer {
    
    var id: Int?
}
